import { Toast } from "vant";
import { addAddress, addOrder, getAddress } from "../../request/api";

const state = {
    data: {
        uid: "",
        username: "",
        userphone: "",
        useraddress: "",
        countmoney: "",
        countnumber: "",
        addtime: "",
        idstr: "",
    },
    address: {},
    addresslist: [],
    oneaddress:{},


}

// 2,声明mutations
const mutations = {
    // 专门来修改goods数据
    changedata(state, data) {
        state.data = data
    },
    changeusername(state, data) {
        state.data.username = data
    },
    changeuserphone(state, data) {
        state.data.userphone = data
    },
    changeuseraddress(state, data) {
        state.data.useraddress = data
    },
    changeuid(state, data) {
        state.data.uid = data
    },
    changeaddtime(state, data) {
        state.data.addtime = data
    },
    changecountmoney(state, data) {
        state.data.countmoney = data
    },
    changeidstr(state, data) {
        state.data.idstr = data
    },
    changecountnumber(state, data) {
        state.data.countnumber = data
    },
    changeAddress(state, data) {
        state.address = data
    },
    changeAddressList(state, data) {
        state.addresslist = data
    },
    oneaddress(state,data){
        state.oneaddress=data
    }
 


}

// 3.声明actions
const actions = {
    dataActions(context, obj) {
        context.commit('changeusername', obj.username)
        context.commit('changeuserphone', obj.userphone)
        context.commit('changeuseraddress', obj.useraddress)
        context.commit('changeuid', obj.uid)
        //    console.log(context.data);
    },
    datasActions(context, obj) {
        //    console.log(obj);
        context.commit('changeaddtime', obj.addtime)
        context.commit('changecountmoney', obj.countmoney)
        context.commit('changeidstr', obj.idstr)
        context.commit('changecountnumber', obj.countnumber)
        //    console.log(context.state.data);
    },
    addorderActions(context, obj) {//添加订单
        addOrder(obj).then(res => {
            if (res.code == 200) {
                Toast(res.msg)
                // console.log(res);
            }
        })
    },
    addressaddActions(context, obj) {//添加地址
        addAddress(obj).then(res => {
            if (res.code == 200) {
                Toast(res.msg)
                // console.log(res);
            }
        })
    },
    addresslistActions(context) {//地址列表
        let uid = JSON.parse(sessionStorage.getItem('user')).uid
        //    console.log(a);
        getAddress({ uid }).then(res => {
            //    console.log(res);
            if (res.code == 200) {
                Toast(res.msg)
                // console.log(res.list);
                let arr = []
                res.list.forEach(item => {
                    let obj = {}
                    obj.name = item.username,
                        obj.tel = item.userphone,
                        obj.address = item.location + item.useraddress,
                        obj.id = item.id,
                        // obj.isDefault=item.status
                    arr.push(obj)
                })
                context.commit('changeAddressList', arr)

            }
        })
    },
    // chosenAddressIdAction(context,data){
    //     context.commit('changechosenAddressId',data)
    //     console.log(context.state.chosenAddressId);
    // },
    oneActions(context,obj){
        context.commit('oneaddress',obj)
    }

}
// 4.声明getters
const getters = {
    data(state) {
        return state.data
    },
    address(state) {
        return state.address
    },
    addresslist(state) {
        return state.addresslist
    },
    oneaddress(state) {
        return state.oneaddress
    }
    // chosenAddressId(state){
    //     return state.chosenAddressId
    // }
}

export default {
    state,
    mutations,
    actions, getters,
    namespaced: true,//使用命名
}